﻿using System;
using System.Collections.Generic;

using Annotations;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Integration.Tests
{
    [TestClass()]
    public class CsvDeserializerTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public class TestObject
        {
            public int Id { get; set; }

            public DateTime Date { get; set; }

            public string Name { get; set; }

            [Length(10)]
            public string Comments { get; set; }
        }

        [TestMethod()]
        [ExpectedException(typeof(InvalidOperationException))]
        public void CsvDeserializerDeserializeWithExceptionTest()
        {
            List<CsvDeserializerFieldMapping> mapping = new List<CsvDeserializerFieldMapping>
            {
                new CsvDeserializerFieldMapping
                {
                    Index = 0,
                    Name = "ID",
                    PropertyName = "Id"
                },
                new CsvDeserializerFieldMapping
                {
                    Index = 1,
                    Name = "DATE",
                    PropertyName = "Date"
                },
                new CsvDeserializerFieldMapping
                {
                    Index = 2,
                    Name = "NAME",
                    PropertyName = "Name"
                },
                new CsvDeserializerFieldMapping
                {
                    Index = 3,
                    Name = "COMMENTS",
                    PropertyName = "Comments"
                }
            };

            CsvDeserializer<TestObject> deserializer = new CsvDeserializer<TestObject>
            {
                Mapping = mapping
            };

            IList<string> row = new List<string>
            {
                "111",
                "2008-07-12 12:12:00",
                "Doe, Joe",
@"Sent an email: return email - he wrote ""Thanks.  Will let you know.""
(Recorded 07/12/2008 12:12 pm)"
            };

            TestObject testObject = deserializer.Deserialize(row);

            Assert.AreEqual(111, testObject.Id);
            Assert.AreEqual("7/12/2008 12:12:00 PM", testObject.Date.ToString());
            Assert.AreEqual("Doe, Joe", testObject.Name);
            Assert.AreEqual(
@"Sent an email: return email - he wrote ""Thanks.  Will let you know.""
(Recorded 07/12/2008 12:12 pm)", 
                testObject.Comments);

        }

        [TestMethod()]
        public void CsvDeserializerDeserializeWithTruncationTest()
        {
            List<CsvDeserializerFieldMapping> mapping = new List<CsvDeserializerFieldMapping>
            {
                new CsvDeserializerFieldMapping
                {
                    Index = 0,
                    Name = "ID",
                    PropertyName = "Id"
                },
                new CsvDeserializerFieldMapping
                {
                    Index = 1,
                    Name = "DATE",
                    PropertyName = "Date"
                },
                new CsvDeserializerFieldMapping
                {
                    Index = 2,
                    Name = "NAME",
                    PropertyName = "Name"
                },
                new CsvDeserializerFieldMapping
                {
                    Index = 3,
                    Name = "COMMENTS",
                    PropertyName = "Comments",
                    TruncateToFit = true
                }
            };

            CsvDeserializer<TestObject> deserializer = new CsvDeserializer<TestObject>
            {
                Mapping = mapping
            };

            IList<string> row = new List<string>
            {
                "111",
                "2008-07-12 12:12:00",
                "Doe, Joe",
@"Sent an email: return email - he wrote ""Thanks.  Will let you know.""
(Recorded 07/12/2008 12:12 pm)"
            };

            TestObject testObject = deserializer.Deserialize(row);

            Assert.AreEqual(111, testObject.Id);
            Assert.AreEqual("7/12/2008 12:12:00 PM", testObject.Date.ToString());
            Assert.AreEqual("Doe, Joe", testObject.Name);
            Assert.AreEqual("Sent an em", testObject.Comments);

        }
    }
}
